package LuceneIndexCreate;

import com.alibaba.fastjson.JSON;
import lucene.IKAnalyzer5x.IKAnalyzer5x;
import LuceneIndexCreate.http.Http;
import LuceneIndexCreate.model.CatData;
import LuceneIndexCreate.model.CatIdsFrom1688;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by User on 2017/12/6.
 */
public class NumiidGet {

    public static void main(String[] args) throws IOException, InterruptedException {


    }

    private static void dsa() {
        String s = "__sw_newuod_count__=1; _bl_uid=FRjhpaX9vqb29a29kmU4uaC0Im6h; __sw_newuno_count__=1; _uab_collina=151273443773292311180325; ali_beacon_id=115.196.139.213.1475666246706.714021.4; l=Al1dZXp9JRnD9VEAaq/RH15p7TNWQJHZ; cna=jCFpEFLgvRACAbedRVqSkA0b; UM_distinctid=15fb5a5d1ccb35-0bb6cd1567205f-5e193d17-1fa400-15fb5a5d1cd991; webp=1; _m_h5_tk=d481e47db5425d05d2ba2a05850bb509_1512541437118; _m_h5_tk_enc=d5da258dabf7541a9fec4adba884d4ab; ali_apache_track=\"c_ms=1|c_mid=b2b-2161592039|c_lid=%E6%9C%B1%E4%B9%BE%E5%8A%9B\"; _cn_slid_=y7kBAiAWJg; last_mid=b2b-2161592039; __last_loginid__=\"%E6%9C%B1%E4%B9%BE%E5%8A%9B\"; _umdata=0712F33290AB8A6D64100FA911E6879425991546EA056039331FBB13348060C431704403519010BECD43AD3E795C914CA596D64F1F255BD46B1D9FEBF4468FF9; h_keys=\"%u660e%u661f%u7eaa%u5ff5%u54c1#%u7b14%u8bb0%u672c#%u53e4%u8463%u949f%u8868#%u7535%u6e90%u9002%u914d%u5668#%u789f\\\\\\\\%u7535%u6c60\\\\\\\\%u7535%u6e90%u9002%u914d%u5668#%u789f\\\\%u7535%u6c60\\\\%u7535%u6e90%u9002%u914d%u5668#%u4e2d%u88e4/%u4e94%u5206%u88e4#%u4e2d%u88e4\\\\/%u4e94%u5206%u88e4#%u7b14%u8bb0%u672c%u7535%u8111#%u789f\"; JSESSIONID=xc0ZxMc-b9qYafMR9nm02F9Yd8-KFGeHdQ-hq4N; alicnweb=touch_tb_at%3D1512796141445%7Clastlogonid%3D%25E6%259C%25B1%25E4%25B9%25BE%25E5%258A%259B%7ChomeIdttS%3D15686904835935605040946498468641069254%7ChomeIdttSAction%3Dtrue%7Cshow_inter_tips%3Dfalse; __cn_logon__=false; _tmp_ck_0=\"7yvliT2OxKpgXqraKKg1IaLd5gcF9UJ81P1aQFR%2BUvpgTujLaZtiLK%2F3SeUn%2Bbb6RyHMcdudgVZqF8%2BlrbrmVk%2Fx6Z2MbusCaYpy%2BjqlufxYLcAqb25w4%2B6rQDXBR2Sfi9LiQ7mGyJ%2BwNc3Osk%2B3SVSzHGfp89Ip%2FfqismQO%2BbR79xILrKqqTE0pphj9siRpZBOWY%2FrKbaygQLABOwqfoKvOW4xgDR2BTxbfkzysLvVrQHizqAZ24P0CKJRuj7waQh8Cg8lKvAY5NYimoCE%2Bjz0CNUV2dcMVbkT53sN4TOybiTnehW%2FvbQ9iUaN5VER5odGL%2FEgT3g3pzWOnb3CvFPJs2UQmL6JaeTAgbBa6jXxR3ED%2Bq743LxFVL3cP%2B%2FjA8Qb9gyjjwww%3D\"; alisw=swIs1200%3D1%7C; ali_ab=115.196.139.213.1475647300457.1; ad_prefer=\"2017/12/09 13:14:33\"; isg=ApeXulF2gcVEiAiWOL6FLBCBJgshdGXULrEAZ-nE82bNGLda8az7jlV6Do79";

        Map map = new HashMap();
        for (String ss : s.split(";")) {
            int i = ss.indexOf('=');
            if (i != -1)
                map.put(ss.substring(0, i), ss.substring(i + 1));
        }
        System.out.println(JSON.toJSONString(map));
    }

    private static void asd() throws UnsupportedEncodingException, InterruptedException {
        String originWords = "洗护清洁剂/卫生巾/纸/香薰-->洗发沐浴/个人清洁-->洗发水";

        List<String[]> list = new ArrayList();
        for (String s : originWords.split("-->")) {
            list.add(s.split("/"));
        }
        List<String> words = new ArrayList();
        combination(list, "", 0, words);

        for (String word : words) {

            /*String keyword = spiltWords(word);
            System.out.println(keyword);*/

            CatData catData = new CatData("123", "数码摄像机");

            CatIdsFrom1688 catIdsFrom1688 = requestMoblieCatIdsFrom1688ByCatData(catData);

            System.out.println(JSON.toJSONString(catIdsFrom1688));
            break;
        }
    }


    private static void combination(List<String[]> list, String s, int i, List result) {
        if (i >= list.size()) {
            result.add(s.trim());
            return;
        }
        for (String ss : list.get(i)) {
            combination(list, (s + " " + ss), (i + 1), result);
        }
    }

    private static String spiltWords(String words) throws IOException {
        Analyzer analyzer = new IKAnalyzer5x(true);
        TokenStream tokenStream = analyzer.tokenStream("test", words);
        OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
        tokenStream.reset();
        List list = new ArrayList();
        while (tokenStream.incrementToken()) {
            list.add(offsetAttribute.toString());
        }

        return String.join(" ", list);
    }

    public static CatIdsFrom1688 requestCatIdsFrom1688ByCatData(CatData catData) throws UnsupportedEncodingException, InterruptedException {
        String cid = catData.getCid();
        String name = catData.getName();
        String keyword = name;
        if (name.contains("/")) {
            keyword = keyword.replace('/', ' ');
        }


        String gbk = URLEncoder.encode(keyword, "GBK");
        String content = null;
        try {
            content = Http.sendGet("https://s.1688.com/selloffer/offer_search.htm?keywords=" + gbk + "&button_click=filtbar&earseDirect=false&n=y&from=taoSellerSearch");
        } catch (Exception e) {
            System.out.println("retry " + keyword);
            return requestCatIdsFrom1688ByCatData(catData);
        }

        /**
         * var coaseParam = {
         'object_ids':'536431393978,5,1;544138732962,302,2;548484396469,302,3;1133033996,302,4;521031718514,2,5;558521690820,2,6;560827438869,2,7;557859888867,2,8;40859418492,2,9;551692086509,2,10;560259359512,2,11;37349472893,2,12;39127315992,103,13;554495493721,104,14;552918789695,1,15;553937932341,103,16;544181718112,302,17;562011221789,1,18;549190984166,1,19;522643266556,1,20;',
         'category_id':'',
         'keyword':'�ʼǱ�����',
         'page_size':'60',
         'page_no':'1',
         'gold_ad_ids':'',
         'isHasGoldAds':false,
         'fnType':'offer',
         'themeId':'',
         'algoResultType':'',
         'coaseType':'specificSpider'
         };
         */
        Pattern pattern = Pattern.compile("'object_ids':'(.*);',\\s'category_id");
        Matcher matcher = pattern.matcher(content);
        String p4pAttrs = null;
        if (matcher.find()) {
            p4pAttrs = matcher.group(1);
        } else {
            System.out.println("retry " + keyword);
            Thread.sleep(60000);
            return requestCatIdsFrom1688ByCatData(catData);
        }


        List<String> result = new ArrayList<>();
        if (!StringUtils.isEmpty(p4pAttrs)) {
            for (String a : p4pAttrs.split(";")) {
                a = a.substring(0, a.indexOf(","));
                result.add(a);
            }
        }

        return new CatIdsFrom1688(cid, name, result);
    }

    public static CatIdsFrom1688 requestMoblieCatIdsFrom1688ByCatData(CatData catData) throws UnsupportedEncodingException, InterruptedException {
        String cid = catData.getCid();
        String name = catData.getName();
        String keyword = name;
        if (name.contains("/")) {
            keyword = keyword.replace('/', ' ');
        }


        String k = URLEncoder.encode(keyword, "utf-8");
        try {
            // ws-mod ws-offerresult ws-offerresult-list
            Connection connect = Jsoup.connect("http://search.m.1688.com/page/search.html?keywords="+k+"&sortType=pop");
            Document document = connect.get();

            Elements elements = document.getElementsByAttributeValue("class", "ws-mod ws-offerresult ws-offerresult-list");
            Elements select = elements.eq(0).select("div[class=wsw-offer]");

            List<String> result = new ArrayList<>();
            Iterator<Element> iterator = select.iterator();
            while (iterator.hasNext()) {
                Element next = iterator.next();
                Elements a = next.select("a");
                String href = a.attr("href");
                Pattern pattern = Pattern.compile("/(\\d+).html|&ui=(\\d+)");
                Matcher matcher = pattern.matcher(href);
                if (matcher.find()) {
                    String group = matcher.group(1);
                    if (group == null) group = matcher.group(2);
                    result.add(group);
                }
            }

            return new CatIdsFrom1688(cid, name, result);
        } catch (Exception e) {
            System.out.println("retry " + keyword);
            Thread.sleep(1000);
            return requestMoblieCatIdsFrom1688ByCatData(catData);
        }


    }


}
